﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Profile;
using SharingCenter.Web.Classes;
using SharingCenter.Web.Classes.ServiceLayer;
using SharingCenter.Web.Classes.Profiles;

namespace SharingCenter.Web
{
    public class Global : System.Web.HttpApplication
    {
        private class HttpContextDataStore : IUnitOfWorkDataStore
        {
            public object this[string key]
            {
                get { return HttpContext.Current.Items[key]; }
                set { HttpContext.Current.Items[key] = value; }
            }
        }

        protected void Application_Start(object sender, EventArgs e)
        {
            SharingCenter.Web.Classes.ServiceLayer.UnitOfWorkHelper.CurrentDataStore = new HttpContextDataStore();
            Application["SITE_CONFIG"] = new Dictionary<SiteConfigSection, Dictionary<string, string>>();
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }

        public void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs args)
        {
            WebProfile anonymousProfile = WebProfile.GetProfile(args.AnonymousID);

            WebProfile.Current.Talks.ThreadReadHistory = anonymousProfile.Talks.ThreadReadHistory;

            ////////
            // Delete the anonymous profile. If the anonymous ID is not 
            // needed in the rest of the site, remove the anonymous cookie.

            ProfileManager.DeleteProfile(args.AnonymousID);
            AnonymousIdentificationModule.ClearAnonymousIdentifier();

            // Delete the user row that was created for the anonymous user.
            Membership.DeleteUser(args.AnonymousID, true);

            // Check whether profile exists. if not, add a profile.
            if (ProfileOperations.GetProfile(WebProfile.Current.UserId) == null)
            {
                //ProfileOperations.InitializePrivacy(WebProfile.Current.UserId);
                //ProfileOperations.InitializeAddress(WebProfile.Current.UserId);
                ProfileOperations.InitializeProfile(WebProfile.Current.UserName, string.Empty, string.Empty);
            }

        }

        public override string GetVaryByCustomString(HttpContext context, string custom)
        {
            switch (custom.Trim().ToLower())
            {
                case "userid":
                    try
                    {
                        return WebProfile.Current.UserId.ToString();
                    }
                    catch { return Guid.Empty.ToString(); }
                    break;
            }
            return base.GetVaryByCustomString(context, custom);
        }
    }
}